
#ifndef _SMART_SCOPED_PTR_H_
#define _SMART_SCOPED_PTR_H_

/***************************************************
 * 可以传递的智能指针哦～
 ***************************************************/
template <typename T>
class TScopedPtr
{
private:
	T*		ptr;
	TScopedPtr( TScopedPtr const & );
	TScopedPtr& operator=( TScopedPtr const & );

	typedef TScopedPtr<T>	this_type;
public:
	typedef T				element_type;
	explicit TScopedPtr( T* p = 0 ) : ptr( p ) {}
	explicit TScopedPtr( std::auto_ptr<T> p ) : ptr( p.release() ) {}
	~TScopedPtr()
	{
		if( ptr ) delete ptr;
	}

	void reset( T* p = 0 )
	{
		// catch self-reset error
		ASSERT( p == 0 || p != ptr );
		this_type(p).swap( *this );
	}

	T& operator*() const
	{
		return *ptr;
	}

	T* operator->() const
	{
		return ptr;
	}

	T* get() const
	{
		return ptr;
	}

	operator bool () const
	{
		return ptr != 0;
	}

	operator ! () const
	{
		return ptr == 0;
	}

	void swap( TScopedPtr & b )
	{
		T* tmp = b.ptr;
		b.ptr = ptr;
		ptr = tmp;
	}
};

template <typename T>
inline void swap( TScopedPtr<T> & a, TScopedPtr<T> & b )
{
	a.swap( b );
}

template <typename T>
inline T * get_pointer( TScopedPtr<T> const & p )
{
	return p.get();
}

#endif

